#include"apue.h"
int sockfd;
typedef struct sockaddr* sp;
void snd()
{
	char name[20];
	char buf[255];
	gets(name);
	write(sockfd,name,sizeof(name));
	while(1)
	{
		gets(buf);
		write(sockfd,buf,sizeof(buf));
		if(strcmp(buf,"q")==0)
		{
			exit(0);
		}
	}
}

int main()
{
	pthread_t tid;
	sockfd=socket(AF_INET,SOCK_STREAM,0);
	if(0>sockfd)
	{
		perror("socket");
		return -1;
	}
	struct sockaddr_in addr={AF_INET};
	addr.sin_port=htons(5566);
	addr.sin_addr.s_addr=inet_addr("192.168.2.3");
	int ret=connect(sockfd,(sp)&addr,sizeof(addr));
	if(ret<0)
	{
		perror("connect");
		return -1;
	}

	pthread_create(&tid,NULL,(void*)&snd,NULL);

	char buf[255]={};
	int len;
	while(1)
	{
		
		len = read(sockfd,buf,sizeof(buf));
		if(len > 0)
		{
			buf[len]=0;
			printf("\n %s\n",buf);
		}
	}
}
